package com.hzj.controller;

import com.hzj.pojo.UserInfo;
import com.hzj.utils.IPUtils;
import com.hzj.utils.RedisUtil;
import com.hzj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/platformapi/api/oss/file")
public class FileUploadController {
    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/fileUpload")
    public Result fileUpload(@RequestParam(required = false) String fileHost, @RequestPart(name = "file", required = false) MultipartFile[] photo, HttpServletRequest request) throws IOException {
        String fileName = "";

        UserInfo userInfo = (UserInfo) redisUtil.get("userInfo"+ redisUtil.get("ip"));
        System.out.println("----用户ip----"+IPUtils.getIpAddr(request));
        System.out.println("----用户id----"+userInfo.getId());

        //接收所有的文件不为空的就上传
        if (photo != null && photo.length > 0) {
            for (MultipartFile p : photo) {
                if (!p.isEmpty()) {
                    fileName = p.getOriginalFilename();
                    p.transferTo(new File("D:\\Mr_Huang\\Nginx\\nginx-windows\\nginx-1.18.0\\html\\file\\" + userInfo.getId() + fileName));
                }
            }
        } else {
            return Result.build(201,"上传图片太大或者为空文件");
        }

        System.out.println("上传成功" + userInfo);
        return Result.ok("http://localhost/file/" + userInfo.getId() + fileName);
    }

}



